package com.feshfans.po;

import org.jivesoftware.smack.packet.Presence;
import org.jxmpp.jid.BareJid;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class User{

    private BareJid jid;
    private List<Presence> presenceList = new ArrayList<>();

    public BareJid getJid() {
        return jid;
    }

    public void setJid(BareJid jid) {
        this.jid = jid;
    }

    public List<Presence> getPresenceList() {
        return presenceList;
    }

    public void setPresenceList(Presence ... presences) {
        if(presences!=null){
            for(Presence presence:presences){
                presenceList.add(presence);
            }
        }
    }
    public void setPresenceList(List<Presence> presenceList) {
        if(presenceList!=null){
            this.presenceList.addAll(presenceList);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(jid.toString(), user.jid.toString());
    }

    @Override
    public int hashCode() {
        return Objects.hash(jid.toString());
    }

    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder();
        builder.append(jid.getLocalpartOrNull());
        builder.append("-");

        boolean isOnline = false;
        String status=null;
        for(Presence presence:presenceList){
            if(presence.getType() == Presence.Type.available){
                isOnline = true;
                status = presence.getStatus();
                break;
            }
        }
        if(isOnline){
            builder.append("在线");
            if(status!=null){
                builder.append("-");
                builder.append(status);
            }

        } else {
            builder.append("离线");
        }


        return builder.toString();
    }
}
